/**
 * @file power.h
 * @brief
 *
 * @author morris (a_morris@163.com)
 * @version v0.0.1
 * @date 2022-01-14
 *
 *
 * @htmlonly <span style="font-weight: bold">History</span> @endhtmlonly
 *
 * Version|Describe|Auther|Date|comment
 * ------|------|------|------|------
 * v0.0.1|create file|morris|2022-01-14|create file
 * v0.3.0|create file|morris|2024-03-09|create file
 *
 *
 * @copyright <h2><center>Copyright &copy; 2024 All Rights Reserved.</center></h2>
 */
#ifndef __BSP_POWER_H__
#define __BSP_POWER_H__

#include <stdint.h>

#define fetl_on()              Gpio_ClrIO(FETL_PORT, FETL_PIN)
#define fetl_off()             Gpio_SetIO(FETL_PORT, FETL_PIN)

#define fetr_on()              Gpio_ClrIO(FETR_PORT, FETR_PIN)
#define fetr_off()             Gpio_SetIO(FETR_PORT, FETR_PIN)

/* 模拟负载开关 */
#define load_on()              Gpio_ClrIO(LOAD_PORT, LOAD_PIN)
#define load_off()             Gpio_SetIO(LOAD_PORT, LOAD_PIN)

#define dir_drv_on()           Gpio_SetIO(DIR_DRV_PORT, DIR_DRV_PIN)
#define dir_drv_off()          Gpio_ClrIO(DIR_DRV_PORT, DIR_DRV_PIN)

#define dir_detl_get_state()   Gpio_GetInputIO(DIR_DETL_PORT, DIR_DETL_PIN)

#define POWER_ISOLATION_NORMAL 0
#define POWER_ISOLATION_LEFT   1
#define POWER_ISOLATION_RIGHT  2

#define POWER_IN_OFF           0
#define POWER_IN_LEFT          1
#define POWER_IN_RIGHT         2

/**
 * @brief 隔离器初始化
 *
 */
void power_init(void);

/**
 * @brief 打开全部隔离器, 进入正常监控状态
 *
 */
void power_on(void);

void power_on_in_dir(void);

/**
 * @brief 关闭右端隔离器, 用于编址
 *
 */
void power_right_off(void);

void power_reset_direction(void);
/**
 * @brief
 *
 */
void power_set_direction(void);

void power_set_drv_on(void);

/**
 * @brief 检测来电方向
 *
* @attention 不要在闭合隔离器的情况下检测
 *
 * @return int
 */
int power_check_direction(void);

/**
 * @brief 获取来电方向值
 *
 * @return int
 */
uint8_t power_get_direction(void);

/**
 * @brief 检测来电方向, 并闭合来电方向端的隔离器
 *
 * @attention 该函数延时1ms, 仅用于冷启动检测来电方向, 不要用于短路隔离判断
 *
 * @return int
 */
int power_in_direction(void);

/**
 * @brief 尝试隔离
 *
 * @attention 这个函数阻塞5ms. 注意操作
 *
 * @return int
 */
int power_try_peer(void);

/**
 * @brief 获取隔离器状态
 *
 * @return uint8_t
 */
uint8_t power_isolation_state(void);

void power_isolation_state_set(uint8_t state);

/**
 * @brief 隔离双端隔离器
 *
 */
void power_isolation_all(void);

/**
 * @brief 获取vcap电压
 *
 * @return uint16_t
 */
uint16_t power_get_vcap_voltage(void);

#endif /* __BSP_POWER_H__ */
